const app = getApp();
const currentTime = new Date().toLocaleDateString();// 当前日期
const WxNotificationCenter = require('../../../common/utils/WxNotificationCenter/WxNotificationCenter');
WxNotificationCenter.postNotificationName(app.INJECT_BLOBAL_EVENT, {
  global: require('../../global'),
  module: 'ws_party_const',
});
const {
  $util,
} = app;
Page({
  data: {
    bannerList: [],
    noticeList: [],
    newsList: [],
    backlog: {},
    navList: [],
    leng: 0,
    showBirAds: false, // 生日提醒
    page: 1,
    hasMore: true,
    swiperCurrent: 0,
    navSwiperCurrent: 0,
    isShowLoading: true, // 是否显示loading
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    isNone: false,
    imgUrl: $util.getImgUrl(),
    contactInfo: {},
  },
  onLoad() {
    this.getBannerList();
    this.setData({
      leng: app.siteInfo.appJson.length ? app.siteInfo.appJson.length : 0,
    });
  },
  onShow() {
    // 生日消息提醒
    const birAdsTime = wx.getStorageSync('birAdsTime') || 0;
    if (currentTime === birAdsTime) {
      this.setData({
        showBirAds: false,
      });
    }
    this.getNoticeList(); // 公告
    this.getContact();// 客服
  },
  init() {
    this.getBirthdayInfo();
    this.getNewsList();
    this.getNavList();
  },
  /**
   * 获取轮播图
   */
  getBannerList() {
    $util.request({
      url: 'entry/wxapp/system.slide.index',
      data: {
        type: 0,
      },
      showLoading: false,
      success: (res) => {
        let isSaaS = res.isThirdApp || false;
        wx.setStorageSync('isSaaS', isSaaS);

        if (isSaaS) {
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: '#B81C24',
          });
        }

        this.setData({
          bannerList: res.data,
          page: 1,
          isNone: res.data.length > 0 ? false : true,
          isSaaS: res.isThirdApp || false,
        }, () => {
          this.init();
        });
      },
    });
  },
  /**
   * 获取公告
   */
  getNoticeList() {
    $util.request({
      url: 'entry/wxapp/system.notice.index',
      data: {},
      showLoading: false,
      success: (res) => {
        this.setData({
          noticeList: res.data,
          noticeset: res.noticeset, // noticeset中roll：1垂直滚动，2水平滚动  page：1跳转到列表，2跳转到详情
          backlog: res.backlog,
          isNone: res.data.length > 0 ? false : true,
        });
      },
    });
  },
  toNotice(e) { 
    // 跳转公告
    let list_url = '/ws_party_const/pages/notice/index';
    let detail_url = '/ws_party_const/pages/notice-detail/notice-detail';
    let id = e.currentTarget.dataset.id;
    let { noticeset } = this.data;
    let url = noticeset && noticeset.page && noticeset.page == 2 ? detail_url : list_url;
    app.util.navigateTo(url, {
      id: noticeset && noticeset.page && noticeset.page == 2 ? id : '',
    });
  },
  // 党员生日消息提醒
  getBirthdayInfo() {
    $util.request({
      url: 'entry/wxapp/index.index.ispbirthday',
      data: {},
      showLoading: false,
      success: (res) => {
        const birAdsTime = wx.getStorageSync('birAdsTime') || 0;
        if (res.pbirthday === 1 && currentTime !== birAdsTime) {
          this.setData({
            showBirAds: true,
          });
        } else {
          this.setData({
            showBirAds: false,
          });
        }
      },
    });
  },
  /**
   * 获取动态新闻
   */
  getNewsList() {
    let { page } = this.data;
    $util.request({
      url: 'entry/wxapp/information.news.index',
      data: { 
        page, 
        version: 1, 
      },
      showLoading: this.data.isShowLoading,
      success: (res) => {
        let newsList = res.data;
        if (page > 1) {
          newsList = [...this.data.newsList, ...newsList];
        }
        if (page === 1) {
          const pages = getCurrentPages(); // 当前页面
          if (pages[0].route === 'ws_party_const/pages/index/index') {
            wx.setNavigationBarTitle({
              title: res.shopname || '',
            });
          }
        }
        this.setData({
          isShowLoading: false,
          newsList,
          hasMore: res.data.length > 0,
          isNone: res.data.length > 0 ? false : true,
          page: newsList.length > 0 ? page + 1 : page,
          newsset: res.newsset || {},
          wxappname: res.wxappname || '',
          helinTit: res.wxappname == 'helin' && res.title ? res.title : '通知公告',
        });
      },
    });
  },
  /**
   * 获取导航
   */
  getNavList() {
    $util.request({
      url: 'entry/wxapp/system.nav.index',
      data: {
        version: 1,
      },
      showLoading: false,
      success: (res) => {
        this.setData({
          navList: res.data,
          setNum: res.set.num,
          navset: res.set.styleset,
          isNone: res.data.length > 0 ? false : true,
        });
      },
    });
  },
  /**
   * 获取客服设置
   */
  getContact() {
    $util.request({
      url: 'entry/wxapp/index.index.customer_service',
      data: {
        version: 1,
      },
      showLoading: false,
      success: (res) => {
        let { isshow = 0, customericon = '' } = res.info;
        this.setData({
          isshow,
          customericon,
        });
      },
    });
  },
  /**
   * 导航跳转
   */
  commonJump(e) {
    let {
      type,
      path = '',
      appid = '',
      title,
    } = e.currentTarget.dataset.item;
    if (Number(type) === 1) {
      let urlArr = path.split('?');
      let paramArr = {};
      paramArr.barTitle = title;
      if (urlArr.length > 1) {
        let param = urlArr[1].split('&');
        for (let i = 0; i < param.length; i++) {
          paramArr[param[i].split('=')[0]] = param[i].split('=')[1];
        }
      }
      app.util.navigateTo(urlArr[0], paramArr);
    } else if (Number(type) === 2) {
      wx.navigateToMiniProgram({
        appId: appid,
        path,
        envVersion: 'release',
      });
    } else if (Number(type) === 3) {
      const url = `/ws_party_const/pages/web/web?webview_url=${path}`;
      wx.navigateTo({
        url,
      });
    }
  },
  bannerJump(e) {
    let {
      ztype,
      path = '',
      appid = '',
      title,
    } = e.currentTarget.dataset.item;
    if (Number(ztype) === 1) {
      let urlArr = path.split('?');
      let paramArr = {};
      paramArr.barTitle = title;
      if (urlArr.length > 1) {
        let param = urlArr[1].split('&');
        for (let i = 0; i < param.length; i++) {
          paramArr[param[i].split('=')[0]] = param[i].split('=')[1];
        }
      }
      app.util.navigateTo(urlArr[0], paramArr);
    } else if (Number(ztype) === 2) {
      wx.navigateToMiniProgram({
        appId: appid,
        path,
        envVersion: 'release',
      });
    } else if (Number(ztype) === 3) {
      const url = `/ws_party_const/pages/web/web?webview_url=${path}`;
      wx.navigateTo({
        url,
      });
    }
  },
  navigateTo(e) {
    let item = e.currentTarget.dataset.item;
    let helin = this.data.wxappname;

    let detail_url = helin == 'helin' ? '/ws_party_const/pages/notice-detail/notice-detail' : '/ws_party_const/pages/news-detail/news-detail';
    let more_url = helin == 'helin' ? '/ws_party_const/pages/notice/index' : '/ws_party_const/pages/dynamic/index';

    if (item === 'more') {
      app.util.navigateTo(more_url, {
        tab: 'news',
      });
    } else {
      app.util.navigateTo(detail_url, {
        id: item.id,
      });
    }

  },
  onShareAppMessage() {
    return {
      path: '/ws_party_const/pages/index/index',
    };
  },
  hideBirAds() {
    this.setData({
      showBirAds: false,
    });
    wx.setStorageSync('birAdsTime', currentTime);
  },
  birAdvDetail() {
    const url = `/ws_party_const/pages/birthday-reminder/index`;
    wx.setStorageSync('birAdsTime', currentTime);
    app.util.navigateTo(url);
  },
  onReachBottom() {
    this.data.hasMore && this.getNewsList();
  },
  onPullDownRefresh() {
    wx.stopPullDownRefresh();
    this.getBannerList();
  },
  swiperChange(e) {
    this.setData({
      swiperCurrent: e.detail.current,
    });
  },
  navSwiperChange(e) {
    this.setData({
      navSwiperCurrent: e.detail.current,
    });
  },
});